

#import "FAMCameRacaloScanAiViewController.h"

@interface FAMCameRacaloScanAiViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *FAMSelectedImageView;
@property (weak, nonatomic) IBOutlet UIView *FAMBottomView;
@property (weak, nonatomic) IBOutlet UILabel *FAMProgressLabel;
@property (nonatomic, strong)PAGView *FAMPagView;
@property (nonatomic, strong)NSTimer *FAMTimer;
@property (nonatomic, assign)NSInteger FAMProgress;
@end

@implementation FAMCameRacaloScanAiViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.titleTextAttributes = @{
        NSFontAttributeName:[UIFont systemFontOfSize:20],
        NSForegroundColorAttributeName:kWhiteColor
    };
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.titleTextAttributes = @{
        NSFontAttributeName:[UIFont systemFontOfSize:20],
        NSForegroundColorAttributeName:kBlackColor
    };
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.FAMProgress = 0;
    [self FAM_showUi];
    [self FAM_loadData];
}
- (void)dealloc{
    [self.FAMTimer invalidate];
    self.FAMTimer = nil;
}
- (void)FAM_showUi{
    
    self.title = @"calo Scan Ai";
    self.FAMSelectedImageView.image = self.FAMSelectedImage;
    self.FAMBottomView.layer.borderColor = kWhiteColor.CGColor;
    self.FAMBottomView.layer.borderWidth = 1;
    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"scan" ofType:@"pag"];
    self.FAMPagView = [[PAGView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self.view addSubview:self.FAMPagView];
    PAGFile *pagFile = [PAGFile Load:path];
    [self.FAMPagView setComposition:pagFile];
    [self.FAMPagView setRepeatCount:0];
    [self.FAMPagView play];
    self.FAMTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(FAM_progress) userInfo:nil repeats:YES];
}
- (void)FAM_loadData{
    WeakSelf(self);
    [FAMHttpTool FAM_PostFoodIdentifyWithParameters:[NSMutableDictionary dictionaryWithDictionary:@{@"data":@{@"image_key":self.FAMFoodImageModel.image_key}}] success:^(FAMFoodIdentifyModel * _Nonnull model, NSString * _Nonnull msg) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.FAMTimer invalidate];
            weakSelf.FAMTimer = nil;
            if (model.foodList.count == 0) {
                [weakSelf showHint:@"No food detected"];
                [weakSelf.navigationController popToViewController:weakSelf.navigationController.childViewControllers[1] animated:YES];
                return;
            }
            weakSelf.FAMProgressLabel.text = @"100%";
            [weakSelf performSelector:@selector(FAM_nextVC:) withObject:model afterDelay:1];
        });
    } failure:^(NSString * _Nonnull errorInfo) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf showHint:errorInfo];
        });
    }];
}
- (void)FAM_progress{
    self.FAMProgress += 1;
    if (self.FAMProgress == 99) {
        [self.FAMTimer invalidate];
        self.FAMTimer = nil;
    }
    self.FAMProgressLabel.text = [NSString stringWithFormat:@"%ld%%",self.FAMProgress];
}
- (void)FAM_nextVC:(FAMFoodIdentifyModel *)model{
    
    FAMCameFoodResultViewController *vc = [FAMCameFoodResultViewController new];
    vc.FAMSelectedImage = self.FAMSelectedImage;
    vc.FAMFoodModel = model;
    vc.FAMFoodImageModel = self.FAMFoodImageModel;
    vc.FAMCateType = self.FAMCateType;
    [self.navigationController pushViewController:vc animated:YES];
}
@end
